package com.hp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.entity.Department;
import com.hp.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

  @Autowired
  private DepartmentService departmentService;
  @RequestMapping("/listPage")
  public String listPage(Model model, @RequestParam(value = "current",defaultValue = "1") Integer current, @RequestParam(value = "size",defaultValue = "3") Integer size){
     Page<Department> departmentPage = new Page<Department>(current,size);
     departmentPage = departmentService.page(departmentPage, new QueryWrapper<>());
     List<Department> records = departmentPage.getRecords();

      for (int i = 0; i < records.size(); i++) {
          System.out.println(records.get(i));
      }
    model.addAttribute("departList",records);//返回的集合
    model.addAttribute("current",current);//当前页数
    model.addAttribute("departmentPage",departmentPage);//总页数
    return "department/department_list";
  }
    //跳转到添加部门信息
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        /*
         * 两种情况,
         * 1.数据库无数据,直接给个初始的部门编号
         * 2.数据库有数据,需要从数据库从查出最大的部门编号
         * */
        List<Department> department_number = departmentService.list();
        //如果部门数据等于null
        if (department_number!=null){
            //查询部门最大编号
            Integer maxNum= departmentService.selectMaxNum();
            model.addAttribute("departmentNumber",maxNum+1);
        }else{
            //如果为空,返回默认值
            model.addAttribute("departmentNumber","2001");
        }
        return "department/department_add";
    }
    //添加方法
    @RequestMapping("/addDepartment")
    public String addDepartment(Department department,Model model){

        System.out.println(department);
        boolean insert = departmentService.save(department);
        return insert ? "redirect:/department/listPage": "department/department_list";
    }
    //跳转到修改页面
    @RequestMapping("/toUpdete/{id}")
    public String toUpdete(Model model, @PathVariable("id") Integer id){
        // 根据主键id返回对象
        System.out.println(id);
        Department department = departmentService.getBaseMapper().selectById(id);
        model.addAttribute("department",department);
        return  "department/department_update";
    }
    //修改部门信息方法
    @RequestMapping("/updDepartment")
    public String updDepartment(Model model,Department department){
        System.out.println(department);
        boolean b = departmentService.updateById(department);
        System.out.println(b);
        return b?"redirect:/department/listPage":"department/department_update";

    }

    @GetMapping("/delDepartment/{id}")
    public String delDepartment( Model model, @PathVariable String id){

        boolean b = departmentService.removeById(id);
        System.out.println(id);

        if (b){
            model.addAttribute("err","删除成功");
            return "redirect:/department/listPage";
        }else {
            model.addAttribute("err","删除失败");
            return "redirect:/department/listPage";
        }
    }

}
